package com.woniuxy.tools.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.entity.AllArticle;
import com.woniuxy.entity.AllRun;
import com.woniuxy.entity.DTO.ArticleRankItem;
import com.woniuxy.entity.DTO.GrabEvent;
import com.woniuxy.entity.DTO.RankItem;
import com.woniuxy.entity.DTO.RunPostMessageDTO;
import com.woniuxy.enums.ResponseEnum;
import com.woniuxy.service.RankingService;
import com.woniuxy.service.RunService;
import com.woniuxy.service.impl.AllArticleServiceImpl;
import com.woniuxy.util.ResponseData;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RunToolService {
    @Autowired
    private AllArticleServiceImpl articleRankingService;

    @Autowired
    private RankingService rankingService;
    @Autowired
    private RunService runService;

    @Tool(description = "获取赚钱排行榜")
    public String getTop(){
        List<RankItem> topRankings = rankingService.getTopRankings(10);
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(topRankings);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }
    @Tool(description = "获取最新的跑腿帖子信息（默认获取三条）")
    public String getRun(int number){
        List<RunPostMessageDTO> list = runService.getListRun(number);
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(json);
        return json;
    }
    @Tool(description = "接取指定的的跑腿帖子")
    public String acceptRun(Integer userId,String runNumber){
        System.out.println("userId"+userId);
        System.out.println("runNumber"+runNumber);
        //去Redis抢单
        Boolean aBoolean = runService.acceptRun(runNumber, userId);
        if (aBoolean){
            GrabEvent grabEvent = new GrabEvent();
            grabEvent.setTaskId(runNumber);
            grabEvent.setUserId(userId);
            runService.sendGrabEvent(grabEvent);
            return "{\"success\": true, \"message\": \"接取成功\"}";
        }
        return "{\"success\": false, \"message\": \"接取失败\"}";
//        // 创建更新条件
//        UpdateWrapper<AllRun> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.eq("run_number", runNumber);
//        // 设置要更新的字段
//        AllRun updateRun = new AllRun();
//        updateRun.setState(1); // 设置状态为1
//        updateRun.setTakeUserId(userId);
//        // 执行更新操作
//        boolean result = runService.update(updateRun, updateWrapper);
//
//        if (result) {
//            return "{\"success\": true, \"message\": \"接取成功\"}";
//        } else {
//            return "{\"success\": false, \"message\": \"接取失败\"}";
//        }
    }
    @Tool(description = "获取热门帖子排行榜")
    public String getTopArticles(int topN) {
        System.out.println("【DEBUG】RunToolService.getTopArticles 被调用，topN = " + topN);
        List<AllArticle> topRankings = articleRankingService.getTopRankedArticles(topN);

        // 🔥 控制台打印排行榜数据（可选）
        System.out.println("热门帖子排行榜（前 " + topN + " 名）:");
        for (AllArticle article : topRankings) {
            System.out.println("文章ID: " + article.getId() +
                    ", 标题: " + article.getTitle() +
                    ", 浏览量: " + article.getViewCount());
        }

        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(topRankings);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return json;
    }

}
